package com.google.code.swingonfire.component.calendar;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;

public class JCalendarNavigation extends JPanel
{

    private static final long serialVersionUID = 484014204632521216L;

    private final JButton previousButton;
    private final JButton parentButton;
    private final JButton nextButton;

    public JCalendarNavigation(JCalendarModel model)
    {
        this(new JCalendarPreviousAction(model), new JCalendarParentAction(model), new JCalendarNextAction(model));
    }

    public JCalendarNavigation(AbstractJCalendarAction previousAction, AbstractJCalendarAction parentAction,
        AbstractJCalendarAction nextAction)
    {
        super(new BorderLayout());

        setBorder(BorderFactory.createEmptyBorder(12, 16, 0, 16));
        setBackground(Color.WHITE);

        previousButton = createButton(previousAction);
        parentButton = createButton(parentAction);
        nextButton = createButton(nextAction);

        add(previousButton, BorderLayout.WEST);
        add(parentButton, BorderLayout.CENTER);
        add(nextButton, BorderLayout.EAST);
    }

    private JButton createButton(AbstractJCalendarAction action)
    {
        JButton button = new JButton(action);

//        button.setFocusPainted(false);
        button.setBorderPainted(false);
        button.setContentAreaFilled(false);
        button.setOpaque(false);
        button.addFocusListener(new FocusListener()
        {

            @Override
            public void focusLost(FocusEvent e)
            {
                //                JButton button = (JButton) e.getComponent();
                //
                //                button.setBorderPainted(false);
                //                button.setContentAreaFilled(false);
            }

            @Override
            public void focusGained(FocusEvent e)
            {
                //                JButton button = (JButton) e.getComponent();
                //
                //                button.setBorderPainted(true);
                //                button.setContentAreaFilled(true);
            }

        });

        button.addMouseListener(new MouseAdapter()
        {

            @Override
            public void mouseEntered(MouseEvent e)
            {
                JButton button = (JButton) e.getComponent();

                button.setBorderPainted(true);
                button.setContentAreaFilled(true);
            }

            @Override
            public void mouseExited(MouseEvent e)
            {
                JButton button = (JButton) e.getComponent();

                button.setBorderPainted(false);
                button.setContentAreaFilled(false);
            }

        });

        return button;
    }

}
